package com.diankangwo.util;

import com.diankangwo.common.utils.Query;
import com.diankangwo.factory.entity.*;
import com.diankangwo.factory.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 结算账单类
 */

@Component
public class AccountUtil {
    private static AccountUtil accountUtil;
    @Autowired
    private TdFactoryService tdFactoryService;
    @Autowired
    private TdOrderService tdOrderService;
    @Autowired
    private TdAccountService tdAccountService;
    @Autowired
    private TdShoppingcartServices tdShoppingcartServices;
    @Autowired
    private TdOrderShoppingcartServices tdOrderShoppingcartServices;
    @Autowired
    private TdCommodityService tdCommodityService;
    @Autowired
    private TdUserInfoService tdUserInfoService;

    public void setUserInfo(TdFactoryService tdFactoryService, TdOrderService tdOrderService,
                            TdAccountService tdAccountService, TdShoppingcartServices tdShoppingcartServices,
                            TdOrderShoppingcartServices tdOrderShoppingcartServices, TdCommodityService tdCommodityService,
                            TdUserInfoService tdUserInfoService) {
        this.tdFactoryService = tdFactoryService;
        this.tdOrderService = tdOrderService;
        this.tdAccountService = tdAccountService;
        this.tdShoppingcartServices = tdShoppingcartServices;
        this.tdOrderShoppingcartServices = tdOrderShoppingcartServices;
        this.tdCommodityService = tdCommodityService;
        this.tdUserInfoService = tdUserInfoService;
    }

    @PostConstruct
    public void init() {
        accountUtil = this;
        accountUtil.tdFactoryService = this.tdFactoryService;
        accountUtil.tdOrderService = this.tdOrderService;
        accountUtil.tdAccountService = this.tdAccountService;
        accountUtil.tdShoppingcartServices = this.tdShoppingcartServices;
        accountUtil.tdOrderShoppingcartServices = this.tdOrderShoppingcartServices;
        accountUtil.tdCommodityService = this.tdCommodityService;
        accountUtil.tdUserInfoService = this.tdUserInfoService;

    }

    /**
     * 订单定时结算
     */
    public void accountFactory() {

        Map<String, Object> params = new HashMap<>();
        params.put("page", 1);
        params.put("limit", Integer.MAX_VALUE);
        //params.put("orderStatus", PayStatus.success.getName());
        Query query = new Query(params);
        List<TdFactory> tdFactoryList = tdFactoryService.queryList(query);

        //                            _ooOoo_
        //                           o8888888o
        //                           88" . "88
        //                           (| -_- |)
        //                            O\ = /O
        //                        ____/`---'\____
        //                      .   ' \\| |// `.
        //                       / \\||| : |||// \
        //                     / _||||| -:- |||||- \
        //                       | | \\\ - /// | |
        //                     | \_| ''\---/'' | |
        //                      \ .-\__ `-` ___/-. /
        //                   ___`. .' /--.--\ `. . __
        //                ."" '< `.___\_<|>_/___.' >'"".
        //               | | : `- \`.;`\ _ /`;.`/ - ` : | |
        //                 \ \ `-. \_ __\ /__ _/ .-` / /
        //         ======`-.____`-.___\_____/___.-`____.-'======
        //                            `=---='
        //
        //         .............................................
        //                  佛祖保佑             永无BUG
        //          佛曰:
        //                  写字楼里写字间，写字间里程序员；
        //                  程序人员写程序，又拿程序换酒钱。
        //                  酒醒只在网上坐，酒醉还来网下眠；
        //                  酒醉酒醒日复日，网上网下年复年。
        //                  但愿老死电脑间，不愿鞠躬老板前；
        //                  奔驰宝马贵者趣，公交自行程序员。
        //                  别人笑我忒疯癫，我笑自己命太贱；
        //                  不见满街漂亮妹，哪个归得程序员？
        // --------------------------------------------------------
        // 写这段代码的时候，只有上帝和我知道它是干嘛的
        // 现在只有上帝知道

        //查询所有的厂家
        for (int i = 0; i < tdFactoryList.size(); i++) {
            //订单总金额
            float sumMoney = 0F;
            //退款订单总金额
            float refundsumMoney = 0F;
            //运费金额
            float freight = 0F;
            //查询厂家下面所有的商品
            List<TdCommodity> tdCommodityList = tdCommodityService.queryByFactoryId(tdFactoryList.get(i).getId());
            for (int j = 0; j < tdCommodityList.size(); j++) {
                //查询商品的购物车信息
                List<TdShoppingcart> tdShoppingcartList = tdShoppingcartServices.selectByCommId(tdCommodityList.get(j).getId());
                if (tdShoppingcartList.size() > 0) {
                    for (int p = 0; p < tdShoppingcartList.size(); p++) {
                        //查询购物车与订单中间信息
                        List<TdOrderShoppingcart> tdOrderShoppingcartList = tdOrderShoppingcartServices.selectByOrderId(tdShoppingcartList.get(p).getId());
                        //循环中间信息
                        for (int k = 0; k < tdOrderShoppingcartList.size(); k++) {
                            //查询订单下得购物车信息
                            TdOrder tdOrder = tdOrderService.selectByPrimaryKey(tdOrderShoppingcartList.get(k).getOrderId());
                            //判断是否已结算
                            if (tdOrder.getState() == 0) {
                                //查询订单状态是否正确
                                if (tdOrder.getOrderStatus().equals(PayStatus.beforedeliver.getName()) || tdOrder.getOrderStatus().equals(PayStatus.delivering.getName()) ||
                                        tdOrder.getOrderStatus().equals(PayStatus.uncommented.getName()) || tdOrder.getOrderStatus().equals(PayStatus.success.getName())) {

                                    //查询用户信息
                                    TdUserinfo tdUserinfo = tdUserInfoService.selectByUid(tdOrder.getUid());
                                    //是否是端口用户,累计金额
                                    if (tdUserinfo.getIsport()) {
                                        sumMoney += tdCommodityList.get(j).getCommoditypriceuser();
                                    } else {
                                        sumMoney += tdCommodityList.get(j).getCommoditypriceshop();
                                    }
                                }
                                //订单是否是退款订单
                                if (tdOrder.getOrderStatus().equals(PayStatus.refundsuccess.getName()) && tdOrder.getState() == 0) {
                                    refundsumMoney += tdOrder.getTotalfee() / 100;
                                }
                            }
                            tdOrder.setState(1);
                            tdOrderService.updateByPrimaryKey(tdOrder);
                            freight += tdCommodityList.get(j).getFreight();
                        }
                    }
                }
            }
            //创建结算记录
            TdAccount tdAccount = new TdAccount();
            tdAccount.setAccountmoney(sumMoney * (tdFactoryList.get(i).getBalanceproportion() / 100));
            tdAccount.setBalanceproportion(tdFactoryList.get(i).getBalanceproportion());
            tdAccount.setCreatedate(new Date());
            tdAccount.setFactoryid(tdFactoryList.get(i).getId());
            tdAccount.setRefundtmoney(refundsumMoney);
            tdAccount.setSummoney(sumMoney);
            tdAccount.setFreightmoney(freight);
            tdAccount.setState(false);
            tdAccount.setUpdatedate(new Date());
            tdAccountService.insert(tdAccount);
            sumMoney = 0F;
            refundsumMoney = 0F;
            freight = 0F;
        }
    }
}
